package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;

public class BibliotecaApp {

    private HashMap<OptionSelect,String> options = new HashMap<OptionSelect,String>();
    private Boolean loginFlag = false;
    private String username = "";
    private Results result = new ResultsLoggedOut();

    public String displayMessage()
    {
        return "Welcome to Bangalore Public Library:\n";
    }

    public HashMap<OptionSelect,String> listOfOptions()
    {
        options.put(OptionSelect.ListBooks, "View list of books");				//on the assumption that the listings start from 1
        options.put(OptionSelect.ReserveBook, "Reserve a book");
        options.put(OptionSelect.UserDetails, "Check membership details");
        options.put(OptionSelect.ListMovies, "View list of movies");
        options.put(OptionSelect.MovieDetails, "View movie details");
        return options;
    }

    public void getInput(HashMap<OptionSelect,String> map)
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("\nEnter the option number you want to select:\n");
        try
        {
            int key = Integer.parseInt(br.readLine());
            OptionSelect option = OptionSelect.values()[key];
            if(!map.containsKey(option))
            {
                System.out.println("Select a valid option!!");
                getInput(map);
            }
            else
            {
                displayResult(option);
            }
        }
        catch(Exception ex)
        {
            System.out.println("Invalid Format! Please try again.");
            getInput(map);
        }
    }

    public void displayOptions()
    {
        int i=0;
        HashMap<OptionSelect,String> map = listOfOptions();
        while(i<OptionSelect.values().length)
        {
            System.out.println(i+". "+map.get(OptionSelect.values()[i]));
            i++;
        }
        getInput(map);
    }

    public Boolean userLogin(Users user)
    {
        try
        {
            System.out.println("username: ");
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String username = br.readLine();
            System.out.println("password: ");
            br = new BufferedReader(new InputStreamReader(System.in));
            String password = br.readLine();
            if(user.authenticateUser(username, password))
            {
                this.loginFlag=true;
                this.username=username;
                result = new ResultsLoggedIn();
                return true;
            }
            else
            {
                this.loginFlag=false;
                return false;
            }
        }
        catch(Exception ex)
        {
            System.out.println("Sorry, there was some problem. Please try again.");
            userLogin(user);
        }
        return false;
    }

    public void displayResult(OptionSelect option)
    {
        Books book = new Books();
        Movies movie = new Movies();
        Users user = new Users();
        switch(option)
        {
            case ListBooks:
                book.displayBooks();
                break;

            case ReserveBook:
                result.reserveBookResult(this, book, user);
                break;

            case UserDetails:
                result.userDetailsResult(this, user, username);
                break;

            case ListMovies:
                movie.displayMoviesList();
                break;

            case MovieDetails:
                result.moviesDetailsResult(this, movie, user);
                break;

            default:
                System.out.println("Select a valid option!!");
        }

    }

	
/*	public void displayResult(int key)
	{
		Books book = new Books();
		Movies movie = new Movies();
		Users user = new Users();
		switch(key)
		{
			case 1:
				book.displayBooks();
				break;
			
			case 2:
				if(loginFlag)
				{
				try{
					System.out.println("\nEnter the name of book you want to reserve:");
					BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
					String name = br.readLine();
					if(book.reserveBook(name))
					{
						System.out.println( "Thank You! Enjoy the book.\n");
					}
					else
					{
						System.out.println("Sorry, this book is already issued.\n");
					}
				}
				catch(Exception ex)
				{
					System.out.println("Sorry we don't have that book yet.\n");
				}
				}
				else
				{
					System.out.println("\nYou need to login to use this feature:\n");
					if(userLogin(user))
					{
						displayOptions();
					}
					else
					{
						System.out.println("The Username and Password combination you entered is incorrect.");
						System.out.println("Please try again");
						displayResult(2);
					}
						
				}
				break;
			
			case 3:
				if(loginFlag)
				{
					user.displayUserDetails(username);
				}
				else
					System.out.println("Please talk to a Librarian. Thank you.");
					
				break;
			
			case 4:
				movie.displayMoviesList();
				break;
				
			case 5:
				if(loginFlag)
				{
				try{
					System.out.println("\nEnter the name of the movie, you want details: ");
					BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
					String name = br.readLine();
					if(movie.getMovies().containsKey(name))
					{
						movie.displayMoviesDetails(name);
					}
					else
					{
						System.out.println("Sorry, we do not have the listings for this movie.\n");
					}
				}
				catch(Exception ex)
				{
					System.out.println("Sorry we don't have that book yet.\n");
				}
				}
				else
				{
					System.out.println("\nYou need to login to use this feature:\n");
					if(userLogin(user))
					{
						displayOptions();
					}
					else
					{
						System.out.println("The Username and Password combination you entered is incorrect.");
						System.out.println("Please try again");
						displayResult(5);
					}
						
				}
				break;
				
			default:
				System.out.println("Select a valid option!!");
		}
		
	}*/

    public void setUsername(String username) {
        this.username = username;
    }



    public static void main(String[] args) {
        BibliotecaApp app = new BibliotecaApp();
        System.out.println(app.displayMessage());
        app.displayOptions();
    }
}
